<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="选择商城">
                <a-select
                  v-model="queryParam.card_store_id"
                  :filter-option="filterOption"
                  show-search
                  @change="handleSearch"
                >
                  <a-select-option :value="item.id" v-for="item in storeList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="会员卡ID">
                <a-input placeholder="请输入会员卡ID" v-model="queryParam.card_id" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="会员卡名称">
                <a-input placeholder="请输入会员卡名称" v-model="queryParam.title" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="品牌名称">
                <a-input placeholder="请输入会员卡ID" v-model="queryParam.brand_name" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <a-table
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="record => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
      >
        <span slot="store" slot-scope="text, record">
          {{ getStoreName(record.store_id) }}
        </span>

        <span slot="created_at" slot-scope="text, record">
          {{ record.created_at > 0 ? $moment(record.created_at * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handleItem('edit', record)">编辑</a-button>
          <a-divider type="vertical" />
          <a-button type="link" size="small" @click="handleItem('remove', record)">删除</a-button>
        </span>
      </a-table>
    </a-card>

    <a-modal v-model="visible" title="会员卡编辑" @ok="submitForm" :width="500" :maskClosable="false">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-model-item label="背景图" prop="background_pic_url">
          <cyyUploadImg
            imgSizeText=""
            @uploadSuccess="e => (form.background_pic_url = e.data)"
            @deleteImg="form.background_pic_url = ''"
            :imgSrc="form.background_pic_url"
            :showSize="false"
          >
          </cyyUploadImg>
        </a-form-model-item>

        <a-form-model-item label="LOGO" prop="logo_url">
          <cyyUploadImg
            imgSizeText=""
            @uploadSuccess="e => (form.logo_url = e.data)"
            @deleteImg="form.logo_url = ''"
            :imgSrc="form.logo_url"
            :showSize="false"
          >
          </cyyUploadImg>
        </a-form-model-item>

        <a-form-model-item label="卡标题" prop="title">
          <a-input v-model="form.title" placeholder="请输入卡标题" />
        </a-form-model-item>

        <a-form-model-item label="品牌" prop="brand_name">
          <a-input v-model="form.brand_name" placeholder="请输入品牌" />
        </a-form-model-item>

        <a-form-model-item label="服务电话" prop="service_phone">
          <a-input v-model="form.service_phone" placeholder="请输入服务电话" />
        </a-form-model-item>

        <a-form-model-item label="封面摘要" prop="abstract">
          <a-textarea v-model="form.abstract" placeholder="请输入封面摘要" :rows="4" />
        </a-form-model-item>

        <a-form-model-item label="特权说明" prop="prerogative">
          <a-textarea v-model="form.prerogative" placeholder="请输入特权说明" :rows="4" />
          <!-- <div class="tip">
            会员卡特权说明,限制1024汉字。
          </div> -->
        </a-form-model-item>

        <a-form-model-item label="使用说明" prop="description">
          <a-textarea v-model="form.description" placeholder="请输入使用说明" :rows="4" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { wechatMpCardList, wechatMpCardUpdate, wechatMpCardDelete } from '@/api/wechartVipCard'
import { cyyUploadImg } from '@/components/CyyUi/index'

export default {
  components: {
    cyyUploadImg
  },

  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns: [
        {
          title: 'ID',
          dataIndex: 'id',
          width: 80
        },
        {
          title: '商城',
          scopedSlots: {
            customRender: 'store'
          }
        },
        {
          title: '会员卡ID',
          dataIndex: 'card_id'
        },
        {
          title: '会员卡名称',
          dataIndex: 'title'
        },
        {
          title: '品牌名称',
          dataIndex: 'brand_name'
        },
        {
          title: '发布时间',
          dataIndex: 'created_at',
          scopedSlots: {
            customRender: 'created_at'
          },
          sorter: (a, b) => a.created_at - b.created_at,
          width: 200
        },
        {
          title: '操作',
          scopedSlots: {
            customRender: 'action'
          }
        }
      ],

      queryParam: {
        title: '',
        brand_name: '',
        card_id: '',
        card_store_id: '',
        store_id: '-1'
      },
      list: [],

      // 所有商城列表
      storeList: [],

      selectedRowKeys: [],
      visible: false,
      form: {
        background_pic_url: '',
        logo_url: '',
        title: '',
        brand_name: '',
        service_phone: '',
        abstract: '',
        prerogative: '',
        description: ''
      },

      rules: {
        background_pic_url: [{ required: true, message: '请上传背景图', trigger: 'change' }],
        logo_url: [{ required: true, message: '请上传LOGO', trigger: 'change' }],
        title: [{ required: true, message: '卡标题不能为空', trigger: 'blur' }],
        brand_name: [{ required: true, message: '品牌不能为空', trigger: 'blur' }],
        service_phone: [{ required: true, message: '服务电话不能为空', trigger: 'blur' }],
        abstract: [{ required: true, message: '封面摘要简介不能为空', trigger: 'blur' }],
        prerogative: [{ required: true, message: '特权说明不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '使用说明不能为空', trigger: 'blur' }]
      },

      modalLoading: false
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getStoreName() {
      return id => {
        let { storeList } = this
        if (!storeList.length) return ''

        let item = storeList.find(el => el.id === id)
        return item ? item.name : ''
      }
    }
  },

  methods: {
    handleItem(type, item) {
      switch (type) {
        case 'remove':
          this.remove(item.card_id)
          break

        case 'edit':
          this.visible = true
          this.edit(item)
          break
      }
    },

    edit(item) {
      let { background_pic_url, brand_name, logo_url, title, prerogative, wxRes, card_id } = item,
        { abstract } = wxRes.card.member_card.advanced_info.abstract,
        { service_phone, description } = wxRes.card.member_card.base_info

      this.form = {
        background_pic_url,
        logo_url,
        title,
        brand_name,
        prerogative,
        card_id,
        description,
        service_phone,
        abstract
      }
    },

    remove(card_id) {
      this.$confirm({
        title: '提示?',
        content: '是否删除？',
        onOk: () => {
          wechatMpCardDelete({
            card_id
          })
            .then(res => {
              if (res.code !== 0) {
                this.$error({
                  content: res.msg
                })
              } else {
                this.$message.success(res.msg)
                this.getList()
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.modalLoading = false
            })
        },
        onCancel: () => {}
      })
    },

    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    handleSearch(value) {
      let [item] = this.storeList.filter((d, index) => {
        return d.id == value
      })
      this.queryParam.card_store_id = item.id
    },

    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await wechatMpCardList({
        ...queryParam,
        pageSize,
        pageNo
      })
      if (res.code == 0) {
        this.selectedRowKeys = []
        let { totalCount, list } = res.data
        this.total = totalCount
        this.list = list
        this.storeList = res.store_list || []
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 表单提交
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.modalLoading = true
          let { form } = this
          wechatMpCardUpdate(form)
            .then(res => {
              this.modalLoading = false
              if (!res.code) {
                this.$message.success('编辑成功')
                this.getList()
                this.visible = false
                return
              }

              this.$message.error(res.msg)
            })
            .catch(err => {
              this.modalLoading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.popContent {
  .creatTime {
    padding-bottom: 10px;
  }

  .img {
    img {
      margin: 0 10px 10px 0;
      width: 100px;
      height: 100px;
      cursor: pointer;
    }
  }

  video {
    width: 100%;
    height: 400px;
  }

  h1 {
    font-size: 18px;
  }
}
</style>
